package com.gitee.qdbp.jdbc.plugins.impl;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import com.gitee.qdbp.tools.files.PathTools;

/**
 * SQL模板文件扫描接口的Spring实现类
 *
 * @author zhaohuihua
 * @version 20201028
 * @since 3.2.4
 */
public class SpringSqlFileScanner extends BaseSqlFileScanner {

    public SpringSqlFileScanner() {
        super();
    }

    public SpringSqlFileScanner(String folders, String filters) {
        super(folders, filters);
    }

    @Override
    protected List<URL> scanSqlFiles(String[] folders, String filter) throws IOException {
        List<URL> urls = new ArrayList<>();
        for (String item : folders) {
            String resourcePath = "classpath*:" + PathTools.concat(item, "**", filter);
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources(resourcePath);
            if (resources != null && resources.length > 0) {
                for (Resource resource : resources) {
                    urls.add(resource.getURL());
                }
            }
        }
        return urls;
    }

}
